package com.jsz.peini.controller.app;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jsz.peini.bean.user.UserSms;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.mapper.user.UserSmsMapper;

/**
 * 小秘书
 * @author Han Guohong
 */
@Controller
public class MytipController {
	private static final Logger logger = LoggerFactory.getLogger(MytipController.class);
	@Resource
	private UserSmsMapper userSmsMapper;
	
	@RequestMapping(value="/toMytipSms")
	public Object toMytipSms(HttpServletRequest request,String userId){
		request.setAttribute("userId",userId);
		return "mytip/secretary";
	}
	
	@ResponseBody
	@RequestMapping(value="/getMytipSms")
	public Object getMytipSms(HttpServletRequest request,@RequestBody UserSms userSms){
		JSONObject infoMap = new JSONObject();
		try {
			JSONObject obj = PeiniUtils.validToken(userSms.getUserId());
			
			if(obj.getInteger("resultCode") != 1){
				//return obj;
				infoMap.put("Mytipall","");
				infoMap.put("pageNum", 0);
				infoMap.put("pages", 0);
				infoMap.put("pageSize", 10);
				infoMap.put("total", 0);
			}
			String userId = obj.getString("userId");
			
			Page<Object> page = PageHelper.startPage(userSms.getPageNow(), userSms.getPageSize());
			List<LinkedHashMap<String,Object>> Mytipall=userSmsMapper.getMytipAllByUserId(userId);
			
			List<String> idList = new ArrayList<String>();
			for (LinkedHashMap<String, Object> map : Mytipall) {
				idList.add(map.get("smsId").toString());
			}
			if(idList.size() > 0){
				userSmsMapper.cancelSmsRemind(idList);
			}
			
			infoMap.put("Mytipall",Mytipall);
			infoMap.put("pageNum", page.getPageNum());
			infoMap.put("pages", page.getPages());
			infoMap.put("pageSize", page.getPageSize());
			infoMap.put("total", page.getTotal());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			infoMap.put("resultCode",Constants.FAIL );
			infoMap.put("resultDesc", "网络请求失败");
		}
		return infoMap;
	}
	
	
}
